#!/usr/bin/env python
__revision__ = "$Id$"
#-----------------------------------------------------------

def sql_quote ( s ) : return s.replace("'","''").replace('\\','\\\\')
def sql_quote_from ( s , delimiter='\t') :
    return s\
           .replace('\\','\\\\')\
           .replace('\n','\\n')\
           .replace('\r','\\r')\
           .replace(delimiter, '\\'+delimiter)
#-----------------------------------------------------------

def case_in_list ( name , sorted , level=20 ) :
    assert level>1
    if len(sorted) == 0 : return 'FALSE'
    elif len(sorted) == 1 : return name+' = %s' % sorted[0]
    elif len(sorted) <= level :
        return name + ' in('+','.join([str(i) for i in sorted])+')'
    else :
        m = len(sorted)/2
        return """case when %s<%s
        then %s
        else %s end""" % (
            name,sorted[m] ,
            case_in_list(name,sorted[:m],level) ,
            case_in_list(name,sorted[m:],level) )
#===========================================================
